package com.ym.user.test;

import com.ym.user.UserServerApplication;
import com.ym.utils.redis.GeoUtil;
import com.ym.utils.uuid.IdUtils;
import com.ym.utils.uuid.UUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.security.SecureRandom;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * @version v1.0
 * @ProjectName: SpringCloud-Alibaba-YM
 * @ClassName: testRedis
 * @Description: TODO
 * @Author: 晨曦
 * @Date: 2021-1-15 9:58
 */
//@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = UserServerApplication.class)
public class testRedis {

        /** 释放锁lua脚本 */
        private static final String RELEASE_LOCK_LUA_SCRIPT = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";

        @Autowired
        private RedisTemplate<String, Object> redisTemplate;
        @Autowired
        private GeoUtil geoUtil;
        @Test
        public void contextLoads() {
            String lockKey = "123";
            //String uid = IdUtils.simpleUUID();
            String uid ="0101203";
            boolean success = redisTemplate.opsForValue().setIfAbsent(lockKey,uid,3, TimeUnit.MINUTES);
            if (!success){
                System.out.println("锁已存在");
            }
            // 指定 lua 脚本，并且指定返回值类型
            DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(RELEASE_LOCK_LUA_SCRIPT,Long.class);
           //  参数一：redisScript，参数二：key列表，参数三：arg（可多个）
            Long result = redisTemplate.execute(redisScript, Collections.singletonList(lockKey),uid);
            System.out.println(result);
        }

    @Test
    public void add() {
        String cityGeoKey ="city";
        Point point =new Point(116.405285,39.904989);
        Point point1 =new Point(116.305285,39.804989);
        String member ="天津";
        geoUtil.geoAdd(cityGeoKey,point1,member);
    }
    @Test
    public void geoGet() {
        String cityGeoKey ="city";
        Point point =new Point(116.405285,39.904989);
        String member ="天津";
        geoUtil.geoGet(cityGeoKey,member);
    }

    @Test
    public void geoDist() {
        String cityGeoKey ="city";
        Point point =new Point(116.405285,39.904989);
        String member ="北京";
        String member1 ="天津";
        geoUtil.geoDist(cityGeoKey,member,member1, RedisGeoCommands.DistanceUnit.KILOMETERS);
    }

    @Test
    public void nearByXY() {
        String cityGeoKey ="city";
        geoUtil.nearByXY(cityGeoKey,116.405285,40.904989);
    }
    @Test
    public void nearByPlace() {
        String cityGeoKey ="city";
        String member1 ="天津";
        geoUtil.nearByPlace(cityGeoKey,member1,105);
    }
    @Test
    public void geoHash() {
        String cityGeoKey ="city";
        String member1 ="天津";
        geoUtil.geoHash(cityGeoKey,member1);
    }
}
